package org.mms.controller;

import org.mms.pojo.dto.PermissionDto;
import org.mms.pojo.entity.Permission;
import org.mms.pojo.request.PermissionUpdateRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.pojo.dto.UserDto;
import org.mms.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/permissions")
@RequiredArgsConstructor
public class PermissionController {
    private final PermissionService permissionService;

    @GetMapping
    public ResponseMessage<List<PermissionDto>> getAll() {
        return ResponseMessage.success(permissionService.findAll()
                .stream()
                .map(Permission::convertToDto)
                .toList());
    }

    @PutMapping
    public ResponseMessage<List<PermissionDto>> update(@RequestBody PermissionUpdateRequest request) {
        return ResponseMessage.success(permissionService.update(request.getUserId(), request.getPermissions())
                .stream()
                .map(Permission::convertToDto)
                .toList());
    }
}